package
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLRequestMethod;
	import flash.net.URLVariables;

	public class FacebookService extends EventDispatcher
	{
		private var _request:URLRequest;
		private var _loader:URLLoader;

		private var _serviceURL:String ="http://localhost:8888/facebookgame/services/index.php";
 		private var _appAPIkey:String="3e99105ed5c922110fb95eeb507e1599";
 		private var _appSecret:String ="fa104a2e1f7d83328599406adbe7cbf1";
 		
 		
 		private var _sessionKey:String
 		
 		public function FacebookService()
		{	
			_request = new URLRequest(_serviceURL);
			_loader = new URLLoader();
			_loader.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
            _loader.addEventListener(Event.COMPLETE, loaderCompleteHandler);

			_request.method = URLRequestMethod.POST;
			//_sessionKey = sessionKey;
			this.connect();
			
		
		}
		private  function connect()
		{
			
			//ตรงนี้ต้องเขียนดึง เอา sesion key ให้ได้
			//แต่ตอนนี้ขอ fig เอาก่อนเพราะเราจะ test กันใน localhost ก่อน
			_sessionKey = "2.cCVM1g9rheIFWPSqEc_D6g__.86400.1263146400-1049931453";
		
		} 
		
		
		public function getFriends():void
		{
			var variable:URLVariables = new URLVariables();
			variable.sessin_key = _sessionKey;
			//variable.uid;
			variable.method ="get.freinds";
			
			this._request.data = variable;
			
			try {
                _loader.load(_request);
            }
            catch (error:SecurityError)
            {
                trace("A SecurityError has occurred.");
            }

		}
		private function configureListeners(dispatcher:IEventDispatcher):void {
           /* dispatcher.addEventListener(Event.COMPLETE, completeHandler);
            dispatcher.addEventListener(Event.OPEN, openHandler);
            dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
            dispatcher.addEventListener(HTTPStatusEvent.HTTP_STATUS, httpStatusHandler);
            dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);*/
        }
        

       private function loaderCompleteHandler(event:Event):void 
       {
       	 try 
           {
                var xml:XML = new XML(_loader.data);
                if(xml.method =="get.friends")
                {
	                var e:FacebookServiceEvent= new FacebookServiceEvent("getFriends");
	                e.result = xml;
	                this.dispatchEvent(e);
                }
                
                
             }catch(e:TypeError)
             {
                    trace("Could not parse the XML file.");
              }
        }
     
		private function errorHandler(e:IOErrorEvent):void {
           
           trace("Had problem loading the XML File.");
           
        }

        


		
		

	}
}